package com.itheima.controller.sms;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.core.PageInfo;
import com.itheima.core.R;
import com.itheima.pojo.sms.SmsHomeRecommendProduct;
import com.itheima.pojo.sms.vo.UpdateVo;
import com.itheima.service.sms.SmsHomeRecommendProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/home/recommendProduct")
@Slf4j
public class SmsHomeRecommendProductController {

    @DubboReference
    private SmsHomeRecommendProductService smsHomeRecommendProductService;

    /**
     * 首页添加推荐
     *
     * @param list
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody List<SmsHomeRecommendProduct> list) {
        log.info("添加首页推荐" + list.toString());

        if (list.size() > 0) {
            for (SmsHomeRecommendProduct smsHomeRecommendProduct : list) {
                if (smsHomeRecommendProduct != null) {
                    smsHomeRecommendProductService.save(smsHomeRecommendProduct);
                }
            }
            return R.success("添加成功", "");
        }
        return R.error("您添加的数据为空，请检查");
    }

    /**
     * 批量删除推荐
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestParam List<Long> ids) {
        log.info("批量删除推荐" + ids.toString());
        if (ids.size() > 0) {
            smsHomeRecommendProductService.removeByIds(ids);
            return R.success("操作成功");
        }
        return R.error("操作失败");
    }

    /**
     * 推荐分页查询
     * @param pageNum
     * @param pageSize
     * @param productName
     * @param recommendStatus
     * @return
     */
    @GetMapping("/list")
    public R list(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        log.info("分页查询推荐" + pageNum + "..." + pageSize + "..." + productName + "..." + recommendStatus);

        PageInfo pageInfo = smsHomeRecommendProductService.pageList(pageNum, pageSize, productName, recommendStatus);

        return R.success("操作成功",pageInfo);
    }

    /**
     * 批量修改状态
     * @param
     * @return
     */
    @PostMapping("/update/recommendStatus")
    public R update(@RequestParam ("ids")List<Long>ids, @RequestParam Integer recommendStatus){
        smsHomeRecommendProductService.updateStatus(ids,recommendStatus);
        return R.success("操作成功");
    }

    /**
     * 修改推荐排序
     * @param id
     * @param sort
     * @return
     */
    @PostMapping("/update/sort/{id}")
    public R updateSort(@PathVariable Long id,@RequestParam Integer sort){
        log.info("修改推荐排序"+id+"..."+sort);
        smsHomeRecommendProductService.updateSort(id,sort);
        return R.success("操作成功");
    }

}
